#ifndef _DRV_SERIAL_STM32F411X_H_
#define _DRV_SERIAL_STM32F411X_H_

#include "stm32f4xx.h"
#include "stm32f4xx_ll_rcc.h"
#include "stm32f4xx_ll_bus.h"
#include "stm32f4xx_ll_spi.h"
#include "stm32f4xx_ll_usart.h"
#include "stm32f4xx_ll_gpio.h"
#include "stm32f4xx_ll_dma.h"
#include "device.h"
#include "serial.h"

#include "drv_gpio.h"

#define STM32F4XX_UART1_TXD_PIN1  {LL_GPIO_PIN_9, GPIOA,LL_GPIO_AF_7,LL_AHB1_GRP1_PERIPH_GPIOA}
#define STM32F4XX_UART1_RXD_PIN1  {LL_GPIO_PIN_10,GPIOA,LL_GPIO_AF_7,LL_AHB1_GRP1_PERIPH_GPIOA}

#define STM32F4XX_UART1_TXD_PIN2  {LL_GPIO_PIN_6, GPIOB,LL_GPIO_AF_7,LL_AHB1_GRP1_PERIPH_GPIOB}
#define STM32F4XX_UART1_RXD_PIN2  {LL_GPIO_PIN_7, GPIOB,LL_GPIO_AF_7,LL_AHB1_GRP1_PERIPH_GPIOB}

#define STM32F4XX_UART2_TXD_PIN1  {LL_GPIO_PIN_5, GPIOD,LL_GPIO_AF_7,LL_AHB1_GRP1_PERIPH_GPIOD}
#define STM32F4XX_UART2_RXD_PIN1  {LL_GPIO_PIN_6, GPIOD,LL_GPIO_AF_7,LL_AHB1_GRP1_PERIPH_GPIOD}

#define STM32F4XX_UART2_TXD_PIN2  {LL_GPIO_PIN_2, GPIOA,LL_GPIO_AF_7,LL_AHB1_GRP1_PERIPH_GPIOA}
#define STM32F4XX_UART2_RXD_PIN2  {LL_GPIO_PIN_3, GPIOA,LL_GPIO_AF_7,LL_AHB1_GRP1_PERIPH_GPIOA}

#define STM32F4XX_UART3_TXD_PIN1  {LL_GPIO_PIN_8, GPIOD,LL_GPIO_AF_7,LL_AHB1_GRP1_PERIPH_GPIOD}
#define STM32F4XX_UART3_RXD_PIN1  {LL_GPIO_PIN_9, GPIOD,LL_GPIO_AF_7,LL_AHB1_GRP1_PERIPH_GPIOD}

#define STM32F4XX_UART3_TXD_PIN2  {LL_GPIO_PIN_10,GPIOB,LL_GPIO_AF_7,LL_AHB1_GRP1_PERIPH_GPIOB}
#define STM32F4XX_UART3_RXD_PIN2  {LL_GPIO_PIN_11,GPIOB,LL_GPIO_AF_7,LL_AHB1_GRP1_PERIPH_GPIOB}

#define STM32F4XX_UART4_TXD_PIN1  {LL_GPIO_PIN_0, GPIOA,LL_GPIO_AF_8,LL_AHB1_GRP1_PERIPH_GPIOA}
#define STM32F4XX_UART4_RXD_PIN1  {LL_GPIO_PIN_1, GPIOA,LL_GPIO_AF_8,LL_AHB1_GRP1_PERIPH_GPIOA}

#define STM32F4XX_UART4_TXD_PIN2  {LL_GPIO_PIN_10,GPIOC,LL_GPIO_AF_8,LL_AHB1_GRP1_PERIPH_GPIOC}
#define STM32F4XX_UART4_RXD_PIN2  {LL_GPIO_PIN_11,GPIOC,LL_GPIO_AF_8,LL_AHB1_GRP1_PERIPH_GPIOC}

#define STM32F4XX_UART5_TXD_PIN1  {LL_GPIO_PIN_12,GPIOC,LL_GPIO_AF_8,LL_AHB1_GRP1_PERIPH_GPIOC}
#define STM32F4XX_UART5_RXD_PIN1  {LL_GPIO_PIN_2, GPIOD,LL_GPIO_AF_8,LL_AHB1_GRP1_PERIPH_GPIOD}

#define STM32F4XX_UART6_TXD_PIN1  {LL_GPIO_PIN_11,GPIOA,LL_GPIO_AF_8,LL_AHB1_GRP1_PERIPH_GPIOA}
#define STM32F4XX_UART6_RXD_PIN1  {LL_GPIO_PIN_12,GPIOA,LL_GPIO_AF_8,LL_AHB1_GRP1_PERIPH_GPIOA}

#define STM32F4XX_UART6_TXD_PIN2  {LL_GPIO_PIN_6,GPIOC,LL_GPIO_AF_8,LL_AHB1_GRP1_PERIPH_GPIOC}
#define STM32F4XX_UART6_RXD_PIN2  {LL_GPIO_PIN_7,GPIOC,LL_GPIO_AF_8,LL_AHB1_GRP1_PERIPH_GPIOC}

#define STM32F4XX_UART7_TXD_PIN1  {LL_GPIO_PIN_8, GPIOE,LL_GPIO_AF_8,LL_AHB1_GRP1_PERIPH_GPIOE}
#define STM32F4XX_UART7_RXD_PIN1  {LL_GPIO_PIN_7, GPIOE,LL_GPIO_AF_8,LL_AHB1_GRP1_PERIPH_GPIOE}

#define STM32F4XX_UART8_TXD_PIN1  {LL_GPIO_PIN_1, GPIOE,LL_GPIO_AF_8,LL_AHB1_GRP1_PERIPH_GPIOE}
#define STM32F4XX_UART8_RXD_PIN1  {LL_GPIO_PIN_0, GPIOE,LL_GPIO_AF_8,LL_AHB1_GRP1_PERIPH_GPIOE}

#define STM32F4XX_UART1_DATA      {USART1, LL_APB2_GRP1_PERIPH_USART1, LL_APB2_GRP1_EnableClock, NULL, NULL, 9,  0, 0x00, USART1_IRQn,NULL,NULL}
#define STM32F4XX_UART2_DATA      {USART2, LL_APB1_GRP1_PERIPH_USART2, LL_APB1_GRP1_EnableClock, NULL, NULL, 9,  0, 0x00, USART2_IRQn,NULL,NULL}
#define STM32F4XX_UART3_DATA      {USART3, LL_APB1_GRP1_PERIPH_USART3, LL_APB1_GRP1_EnableClock, NULL, NULL, 9,  0, 0x00, USART3_IRQn,NULL,NULL}
#define STM32F4XX_UART4_DATA      {UART4,  LL_APB1_GRP1_PERIPH_UART4,  LL_APB1_GRP1_EnableClock, NULL, NULL,10,  0, 0x00, UART4_IRQn,NULL,NULL}
#define STM32F4XX_UART5_DATA      {UART5,  LL_APB1_GRP1_PERIPH_UART5,  LL_APB1_GRP1_EnableClock, NULL, NULL, 9,  0, 0x00, UART5_IRQn,NULL,NULL}
#define STM32F4XX_UART6_DATA      {USART6, LL_APB2_GRP1_PERIPH_USART6, LL_APB2_GRP1_EnableClock, NULL, NULL, 9,  0, 0x00, USART6_IRQn,NULL,NULL}
#define STM32F4XX_UART7_DATA      {UART7,  LL_APB1_GRP1_PERIPH_UART7,  LL_APB1_GRP1_EnableClock, NULL, NULL, 9,  0, 0x00, UART7_IRQn,NULL,NULL}
#define STM32F4XX_UART8_DATA      {UART8,  LL_APB1_GRP1_PERIPH_UART8,  LL_APB1_GRP1_EnableClock, NULL, NULL, 9,  0, 0x00, UART8_IRQn,NULL,NULL}


#define STM32F4XX_UART1_TX_DMA27    {DMA2,LL_DMA_STREAM_7,LL_DMA_CHANNEL_4,DMA2_Stream7_IRQn,10,0}
#define STM32F4XX_UART1_RX_DMA22    {DMA2,LL_DMA_STREAM_2,LL_DMA_CHANNEL_4,DMA2_Stream2_IRQn,11,0}

#define STM32F4XX_UART2_TX_DMA16    {DMA1,LL_DMA_STREAM_6,LL_DMA_CHANNEL_4,DMA1_Stream6_IRQn,10,0}
#define STM32F4XX_UART2_RX_DMA15    {DMA1,LL_DMA_STREAM_5,LL_DMA_CHANNEL_4,DMA1_Stream5_IRQn,11,0}

#define STM32F4XX_UART3_TX_DMA13    {DMA1,LL_DMA_STREAM_3,LL_DMA_CHANNEL_4,DMA1_Stream3_IRQn,10,0}
#define STM32F4XX_UART3_RX_DMA11    {DMA1,LL_DMA_STREAM_1,LL_DMA_CHANNEL_4,DMA1_Stream1_IRQn,11,0}

#define STM32F4XX_UART4_TX_DMA14    {DMA1,LL_DMA_STREAM_4,LL_DMA_CHANNEL_4,DMA1_Stream4_IRQn,10,0}
#define STM32F4XX_UART4_RX_DMA12    {DMA1,LL_DMA_STREAM_2,LL_DMA_CHANNEL_4,DMA1_Stream2_IRQn,11,0}

#define STM32F4XX_UART5_TX_DMA17    {DMA1,LL_DMA_STREAM_7,LL_DMA_CHANNEL_4,DMA1_Stream7_IRQn,10,0}
#define STM32F4XX_UART5_RX_DMA10    {DMA1,LL_DMA_STREAM_0,LL_DMA_CHANNEL_4,DMA1_Stream0_IRQn,11,0}

#define STM32F4XX_UART6_TX_DMA26    {DMA2,LL_DMA_STREAM_6,LL_DMA_CHANNEL_5,DMA2_Stream6_IRQn,10,0}
#define STM32F4XX_UART6_RX_DMA21    {DMA2,LL_DMA_STREAM_1,LL_DMA_CHANNEL_5,DMA2_Stream1_IRQn,11,0}

#define STM32F4XX_UART6_TX_DMA27    {DMA2,LL_DMA_STREAM_7,LL_DMA_CHANNEL_5,DMA2_Stream7_IRQn,10,0}
#define STM32F4XX_UART6_RX_DMA22    {DMA2,LL_DMA_STREAM_2,LL_DMA_CHANNEL_5,DMA2_Stream2_IRQn,11,0}

#define STM32F4XX_UART7_TX_DMA15    {DMA1,LL_DMA_STREAM_1,LL_DMA_CHANNEL_5,DMA1_Stream1_IRQn,10,0}
#define STM32F4XX_UART7_RX_DMA13    {DMA1,LL_DMA_STREAM_3,LL_DMA_CHANNEL_5,DMA1_Stream3_IRQn,11,0}

#define STM32F4XX_UART8_TX_DMA10    {DMA1,LL_DMA_STREAM_0,LL_DMA_CHANNEL_5,DMA1_Stream0_IRQn,10,0}
#define STM32F4XX_UART8_RX_DMA16    {DMA1,LL_DMA_STREAM_6,LL_DMA_CHANNEL_5,DMA1_Stream6_IRQn,11,0}


struct stm32f4xx_dma_data_s{
    DMA_TypeDef                 *dma;
    uint32_t                    stream;
    uint32_t                    channel;
    IRQn_Type                   irq;
    uint8_t                     irq_pre_pri;
	uint8_t                     irq_sub_pri;
};

struct stm32f4xx_uart_data_s{
    USART_TypeDef               *USARTx;
    uint32_t                     clock;
    void                       (*enable_clock)(uint32_t Periphs);
    struct stm32f4xx_gpio_s 	*txd;
	struct stm32f4xx_gpio_s 	*rxd;
    uint8_t                     irq_pre_pri;
	uint8_t                     irq_sub_pri;   
    uint32_t                    new_data_from_int;
    IRQn_Type                   irq;	
    
    struct stm32f4xx_dma_data_s   *dma_tx;
    struct stm32f4xx_dma_data_s   *dma_rx;
};

void stm32f4_serial_register(char *name,struct lt_serial_s *serial,struct stm32f4xx_uart_data_s *uart,uint16_t flag);

#endif
